#pragma once
#include "src/project_settings.h"

#include <QWidget>

#include <QPaintEvent>
#include <QPainter>
#include <QImage>
#include <QPixmap>

#include <QMouseEvent>


struct PointsLabeled
{
	int x0, y0, yf, xf, w, h, cx, cy;

	PointsLabeled()
	{
		clear();
	}

	void clear()
	{
		x0 = y0 = yf = xf = w = h = cx = cy = -1;
	}

	void setP0(int x, int y)
	{
		x0 = x; y0 = y;
	}

	void setP1(int x, int y)
	{
		xf = x; yf = y;
	}

	bool canDraw()
	{
		return xf >= 0 && yf >= 0 && x0 >= 0 && yf >= 0 && xf != x0 && yf != y0;
	}

	QRect rect()
	{
		int x0_ = x0, y0_ = y0, xf_ = xf, yf_ = yf;
		
		if (xf < x0)
		{
			x0_ = xf;
			xf_ = x0;
		}

		if (yf < y0)
		{
			y0_ = yf;
			yf_ = y0;
		}

		return QRect(x0_, y0_, xf_ - x0_, yf_ - y0_);
	}

	void check()
	{
		int x0_ = x0, y0_ = y0, xf_ = xf, yf_ = yf;

		if (xf < x0)
		{
			x0_ = xf;
			xf_ = x0;
		}

		if (yf < y0)
		{
			y0_ = yf;
			yf_ = y0;
		}

		x0 = x0_; y0 = y0_; xf = xf_; yf = yf_;
		w = abs(xf - x0);
		h = abs(yf - y0);

		cx = (x0 + y0) / 2;
		cy = (x0 + y0) / 2;

	}
};


class ImageWidget : public QWidget
{
	Q_OBJECT
public:

	explicit ImageWidget(QWidget *parent = NULL) : QWidget(parent)
	{
		//lbPoint = NULL;
		lbPoint = new PointsLabeled();
		setMouseTracking(true);
	}

	~ImageWidget(void)
	{
	}

protected:


	void paintEvent(QPaintEvent *event)
	{
		QWidget::paintEvent(event);
		if (pix.isNull()) return;


		QPainter painter(this);
		painter.drawPixmap(QPoint(), pix);


		painter.setPen(QPen(QColor(0, 255, 0), 2));

		if (lbPoint != NULL && lbPoint->canDraw())
		{
			QRect rect = lbPoint->rect();

			painter.drawRect(rect);

			painter.setPen(QPen(QColor(0, 0, 255), 2));
			painter.drawEllipse(QPoint(lbPoint->x0, lbPoint->y0), 2, 2);
			painter.setPen(QPen(QColor(255, 0, 0), 2));
			painter.drawEllipse(QPoint(lbPoint->xf, lbPoint->yf), 2, 2);

			QString msg = QString::number(rect.x()).append(",").append(QString::number(rect.y())).append(",").append(QString::number(rect.width())).append(",").append(QString::number(rect.height()));

			emit sendRect(msg);
		}

		return;
	}

	void mousePressEvent(QMouseEvent * event)
	{
		lbPoint->clear();
		lbPoint->setP0(event->x(), event->y());
		pressed = true;
		update();
	}

	void mouseReleaseEvent(QMouseEvent * event)
	{
		lbPoint->setP1(event->x(), event->y());
		pressed = false;
		lbPoint->check();
		update();
	}

	void mouseMoveEvent(QMouseEvent *event)
	{
		emit sendInfo(QString(QString::number(event->pos().x())).append(",").append(QString::number(event->pos().y())));

		if (pressed)
		{
			int x = event->x();
			int y = event->y();

			if (x < 0) x = 0;
			if (x >= w) x = w - 1;

			if (y < 0) y = 0;
			if (y >= h) y = h - 1;

			lbPoint->setP1(x, y);



			update();
		}


	}


private:

	QPixmap pix;
	PointsLabeled* lbPoint;

	QPoint *pf, *pi;
	int w, h;

	bool pressed;


public slots:

void setImage(QPixmap pixmap)
	{
		pix = pixmap;
	//	if (lbPoint != NULL)
	//	{
	//		delete lbPoint;
	//	}
	//	lbPoint = new PointsLabeled();
		w = pix.width(); h = pix.height();
		resize(w, h);

		update();
	}

signals:

	void sendInfo(QString);

	void sendRect(QString);

};

